package com.jinmdz.app.dao.dao.provider;


import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;

/**
 * pda冷藏Provider
 *
 * @author Wangshengtao
 * @date 2020-08-24 10:47
 */
public class ColdStorageProvider {

    /**
     * 按照业务编码查询逝者冷藏信息
     *
     * @param businessCode 业务编码
     * @return
     * @author Wangshengtao
     * @date 2020-08-24 10:51
     */
    public String getColdStorageViewByBusinessCode(@Param("businessCode") String businessCode) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" cs.id coldStorageId");
                builder.append(" ,fd.dead_name ");
                builder.append(" ,cs.business_code ");
                builder.append(" ,cs.equipment_id ");
                builder.append(" ,cs.process_state_code ");
                builder.append(" ,(select data_text from data_dictionary where data_code = process_state_code and dict_code = 'refrigerated_state') processStateText");
                builder.append(" ,fm.member_name ");
                builder.append(" ,fm.mobile_phone ");
                builder.append(" ,cs.equipment_type_code ");
                SELECT(builder.toString());
                FROM(" funeral_dead fd ");
                LEFT_OUTER_JOIN(" cold_storage cs on fd.business_code = cs.business_code ");
                LEFT_OUTER_JOIN(" family_member fm on cs.business_code = fm.business_code ");
                WHERE(" cs.business_code=#{businessCode} and fd.deleted=0 and cs.deleted=0 and fm.deleted=0 ");
                ORDER_BY(" cs.id desc limit 1 ");
            }
        }.toString();
    }

    /**
     * 按照设备编码查询逝者冷藏柜信息
     *
     * @param equipmentCode 设备编码
     * @return
     * @author Wangshengtao
     * @date 2020-08-24 10:51
     */
    public String getColdStorageEquipmentByEquipmentCode(@Param("qrCodeMapData") HashMap<String,Object> qrCodeMapData) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" cs.id coldStorageId");
                builder.append(" ,cse.id equipmentId");
                builder.append(" , cse.disable_state ");
                builder.append(" , cse.equipment_code ");
                builder.append(" , cse.equipment_state ");
                builder.append(" , cse.equipment_type_code ");
                builder.append(" , cs.process_state_code ");
                builder.append(" ,(select data_text from data_dictionary where data_code = process_state_code and dict_code = 'refrigerated_state') process_state_text ");
                SELECT(builder.toString());
                FROM(" cold_storage_equipment cse ");
                LEFT_OUTER_JOIN(" cold_storage cs on cse.id = cs.equipment_id ");
                WHERE(" equipment_code = #{qrCodeMapData.qrCodeData} ");
                WHERE(" cse.deleted=0 ");
                ORDER_BY(" cs.id desc limit 1 ");
            }
        }.toString();
    }
}
